#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_prop3.info <<'%EOF%'
   geoopt_prop3
   ------------
   Molecule:         O3
   Wave Function:    MCSCF (CAS) / aug-cc-pVDZ
   Test Purpose:     Geometry optimization using .OPTIMIZE module
                     (2nd order) with a calculation of quadrupole moment,
                     magnetizability and nuclear shieldings at the
                     optimized geometry, along with a vibrational analysis
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_prop3.mol <<'%EOF%'
BASIS
aug-cc-pVDZ
Geometry optimization, calculation of magnetizabilities
and nuclear shieldings at the optimized geometry
    1    2  Y  Z
        8.    2
OS         .0000000000        0.0000000000         .0000000000             *
OP        1.2634502500        2.0449019100         .0000000000             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_prop3.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.2NDORDER
**WAVE FUNCTIONS
.HF
.MP2
.MCSCF
*SCF INPUT
.DOUBLY OCCUPIED
 6 4 1 1
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 4 2 0 0
.ELECTRONS
 12
.CAS SPACE
 3 3 2 1
**EACH STEP
.VIBANA
**PROPERTIES
.VIBANA
.MAGNET
.SHIELD
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_prop3.check
cat >>geoopt_prop3.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Reading molecular geometry:
CRIT1=`$GREP "1 * x *  (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "4 * x * 1\.2634502500" $log | wc -l`
CRIT3=`$GREP "5 * y * 2\.0449019100" $log | wc -l`
CRIT4=`$GREP "7 * x * 1\.2634502500" $log | wc -l`
CRIT5=`$GREP "8 * y * \-2\.0449019100" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[1]=5
ERROR[1]="MOLECULAR GEOMETRY NOT CORRECT"

# Nuclear repulsion energy:
CRIT1=`$GREP "Nuclear repulsion energy : * 68\.8991880907[0-9][0-9]" $log | wc -l`
TEST[2]=`expr $CRIT1`
CTRL[2]=1
ERROR[2]="NUCLEAR REPULSION ENERGY NOT CORRECT"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: * 3 * 3 * 2 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: * 27 * 21 * 12 * 9" $log | wc -l`
TEST[3]=`expr $CRIT1  \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY IS NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Hartree\-Fock total * energy\: * \-224\.2905002437[0-9][0-9]" $log | wc -l`
TEST[4]=`expr $CRIT1`
CTRL[4]=1
ERROR[4]="INITIAL HF ENERGY NOT CORRECT"

# MCSCF energy, 1st iteration:
CRIT1=`$GREP "MP2 second order energy *\: * \-224\.963913" $log | wc -l`
CRIT2=`$GREP "Final MCSCF energy\: * \-224\.5185239" $log | wc -l`
TEST[5]=`expr $CRIT1  \+ $CRIT2`
CTRL[5]=2
ERROR[5]="INITIAL MCSCF ENERGY IS NOT CORRECT"

# Occupancies, 1st iteration:
# March 2020: order may change ...
#CRIT1=`$GREP "2.000000000 * 2.000000000 * 2.000000000 * 2.000000000 * 1.99018188[0-9]" $log | wc -l`
#CRIT2=`$GREP "1.9572359.[0-9] * (0| ).0602610.[0-9]" $log | wc -l`
#CRIT3=`$GREP "2.000000000 * 2.000000000 * 1.9919876.[0-9] * 1.94878385[0-9] * (0| ).0506602.[0-9]" $log | wc -l`
#CRIT4=`$GREP "1.96364173[0-9] * (0| ).2484929.[0-9]" $log | wc -l`
#CRIT5=`$GREP "1.7887546.[0-9]" $log | wc -l`
#TEST[6]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5`
#CTRL[6]=12
TEST[6]=0
CTRL[6]=0
ERROR[6]="INITIAL OCCUPANCIES NOT CORRECT"

# MCSCF energy, 1st iteration:
CRIT1=`$GREP "Total energy * \-224\.518523910[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-6109\.4598163[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-589473\.293[0-9] kJ\/mol" $log | wc -l`
TEST[7]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="INITIAL ENERGY NOT CORRECT"

# Gradient, 1st iteration:
CRIT1=`$GREP "OS * x * 0*\.0099" $log | wc -l`
CRIT2=`$GREP "OP * x * \-0*\.0099" $log | wc -l`
CRIT3=`$GREP "OP * y * \-0*\.0189" $log | wc -l`
TEST[8]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="INITIAL GRADIENT NOT CORRECT"

# Hessian, 1st iteration:
CRIT1=`$GREP "OS * x * (0| )\.4296" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.4296.. * (0| )\.4296" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.2514.. * (0| )\.2514.. * (0| )\.9240" $log | wc -l`
CRIT4=`$GREP "OS * y * (0| )\.4303" $log | wc -l`
CRIT5=`$GREP "OP * x * (\-0|\-)\.2707.. * (0| )\.1579" $log | wc -l`
CRIT6=`$GREP "OP * y * (\-0|\-)\.4303.. * (0| )\.2707.. * (0| )\.4303" $log | wc -l`
CRIT7=`$GREP "OS * z * (\-0|\-)\.00784" $log | wc -l`
CRIT8=`$GREP "OP * z * (0| )\.00784. * (\-0|\-)\.00784" $log | wc -l`
CRIT9=`$GREP "OP * z * (\-0|\-)\.00927" $log | wc -l`
TEST[9]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
CTRL[9]=10
ERROR[9]="INITIAL HESSIAN NOT CORRECT"

# Dipole moment, 1st iteration:
CRIT1=`$GREP "(0| )\.216(5|6).. * (0| )\.5505.. * 1\.836" $log | wc -l`
CRIT2=`$GREP "x * (\-0|\-)\.216[0-9]* * (\-0|\-)\.5505[0-9]* * \-1\.836" $log | wc -l`
TEST[10]=`expr $CRIT1  \+ $CRIT2`
CTRL[10]=2
ERROR[10]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step, 1st iteration:
CRIT1=`$GREP "OS * (\-0|\-)\.0087[0-9]* * (0| )\.0000000000 * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "OP *_1 * 1\.2678[0-9]* * 2\.06184[0-9]* * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "OP *_2 * 1\.2678[0-9]* * \-2\.06184[0-9]* * (0| )\.0000000000" $log | wc -l`
TEST[11]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="INITIAL STEP NOT CORRECT"

# 2nd iteration:
CRIT1=`$GREP -l "Energy at this geometry is * \: * \-224\.518756" $log | wc -l`
CRIT2=`$GREP -l "Norm of gradient * \: * (0| )\.000(79|80)[0-9]" $log | wc -l`
CRIT3=`$GREP -l "Norm of step * \: * (0| )\.0012(8|9)[0-9]" $log | wc -l`
TEST[12]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[12]=3
ERROR[12]="SECOND ITERATION NOT CORRECT"

# 3rd iteration:
CRIT1=`$GREP -l "Energy at this geometry is * \: * \-224\.51875" $log | wc -l`
CRIT2=`$GREP -l "Norm of gradient * \: * (0| )\.00000" $log | wc -l`
CRIT3=`$GREP -l "Norm of step * \: * (0| )\.0000(0|1)" $log | wc -l`
TEST[13]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[13]=3
ERROR[13]="THIRD ITERATION NOT CORRECT"

# Final geometry:
CRIT1=`$GREP "OS * (\-0|\-)\.009175[0-9]* * (0| )\.0000000000 * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "OP *_1 * 1\.26803[0-9]* * 2\.06268[0-9]* * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "OP *_2 * 1\.26803[0-9]* * \-2\.06268[0-9]* * (0| )\.0000000000" $log | wc -l`
TEST[14]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[14]=12
ERROR[14]="FINAL GEOMETRY NOT CORRECT"

# Final SCF energy:
CRIT1=`$GREP "Total energy * \-224\.51875696" $log | wc -l`
TEST[15]=`expr $CRIT1`
CTRL[15]=1
ERROR[15]="FINAL ENERGY NOT CORRECT"

# Magnetizability:
CRIT1=`$GREP "Isotropic magnetizability\: * 1\.452(3|4) * 114\.(59|60).* * 6\.901." $log | wc -l`
CRIT2=`$GREP "Diamagnetic contribution\: * \-18\.6591 * \-1472\.39.. * \-88\.66.." $log | wc -l`
CRIT3=`$GREP "Paramagnetic contribution\: * 20\.111. * 158(6|7)\..... * 95\.57.." $log | wc -l`
CRIT4=`$GREP "1st anisotropy\: * \-7\.2079 * \-568\.77.. * \-34\.252." $log | wc -l`
CRIT5=`$GREP "2nd anisotropy\: * 7\.12(5|6). * 562\.3(0|1).. * 33\.863." $log | wc -l`
CRIT6=`$GREP "Bx * 1\.5069[0-9]* * (\-0|\-|0| )\.000000000000 * (\-0|\-|0| )\.000000000000" $log | wc -l`
CRIT7=`$GREP "By * (\-0|\-|0| )\.000000000000 * 6\.2028.* * (\-0|\-|0| )\.000000000000" $log | wc -l`
CRIT8=`$GREP "Bz * (\-0|\-|0| )\.000000000000 * (\-0|\-|0| )\.000000000000 * \-3\.3529" $log | wc -l`
TEST[16]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8`
CTRL[16]=8
ERROR[16]="MAGNETIZABILITIES NOT CORRECT"

# Molecular g-tensors:
CRIT1=`$GREP "Isotropic g-tensor * \: * \-1\.01340" $log | wc -l`
CRIT2=`$GREP "Nuclear contribution * \: *  (0| )\.50379" $log | wc -l`
CRIT3=`$GREP "Diamagnetic contribution \: * (\-0|\-)\.12123" $log | wc -l`
CRIT4=`$GREP "Electronic contribution * \: * \-1\.39597" $log | wc -l`
CRIT5=`$GREP "1st anisotropy\: * \-2\.60684" $log | wc -l`
CRIT6=`$GREP "2nd anisotropy\: * 1\.19401" $log | wc -l`
CRIT7=`$GREP "A * \-2\.7513[0-9]* * (\-0|\-|0| )\.00000000 * (\-0|\-|0| )\.00000000" $log | wc -l`
CRIT8=`$GREP "B * (\-0|\-|0| )\.00000000 * (\-0|\-)\.21739[0-9]* * (\-0|\-|0| )\.00000000" $log | wc -l`
CRIT9=`$GREP "C * (\-0|\-|0| )\.00000000 * (\-0|\-|0| )\.00000000 * (\-0|\-)\.07151" $log | wc -l`
TEST[17]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
CTRL[17]=9
ERROR[17]="ROTATIONAL G-TENSOR NOT CORRECT"

# Chemical shielding:
CRIT1=`$GREP "Shielding constant\: * \-620\.57.[0-9] ppm" $log | wc -l`
CRIT2=`$GREP "Anisotropy\: * \-1136\.9(3|4).[0-9] ppm" $log | wc -l`
CRIT3=`$GREP "Asymmetry\: * (0| )\.705[0-9]" $log | wc -l`
CRIT4=`$GREP "S parameter\: * 1227\.54.[0-9] ppm" $log | wc -l`
CRIT5=`$GREP "A parameter\: * (0| )\.0000 ppm" $log | wc -l`
CRIT6=`$GREP "Shielding constant\: * \-1173\.21.[0-9] ppm" $log | wc -l`
CRIT7=`$GREP "Anisotropy\: * \-1778\.46.[0-9] ppm" $log | wc -l`
CRIT8=`$GREP "Asymmetry\: * (0| )\.856[0-9]" $log | wc -l`
CRIT9=`$GREP "S parameter\: * 1984\.1..[0-9] ppm" $log | wc -l`
CRIT10=`$GREP "A parameter\: * 246\.59.[0-9] ppm" $log | wc -l`
CRIT11=`$GREP "\@1 OS * \-620\.575. * 476\.16(6|7).\-1096\.74.. * (\-0|\-)\.2388 1404\.1(6|7)..  969\.31.. * 1\.3459" $log | wc -l`
CRIT12=`$GREP "\@1 OP *_1 *\-1173\.21.. * 446\.085.\-1619\.(2|3)... * (\-0|\-)\.1115 2286\.3... 1651\.0... * 1\.1544" $log | wc -l`
CRIT13=`$GREP "\@1 OP *_2 *\-1173\.21.. * 446\.085.\-1619\.(2|3)... * (\-0|\-)\.1115 2286\.3... 1651\.0... * 1\.1544" $log | wc -l`
TEST[18]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10 \+ \
               $CRIT11 \+ $CRIT12 \+ $CRIT13`
CTRL[18]=18
ERROR[18]="SHIELDINGS NOT CORRECT"

# Vibrational frequencies:
CRIT1=`$GREP "1 * A1 * 1093\.9. * (0| )\.00498[0-9] * (0| )\.79[0-9] * (0| )\.018[0-9]" $log | wc -l`
CRIT2=`$GREP "3 * A1 * 708\.4. * (0| )\.00322[0-9] * 4\.49[0-9] * (0| )\.106[0-9]" $log | wc -l`
CRIT3=`$GREP "2 * B1 * 1021\.9. * (0| )\.00465[0-9] * 89\.00[0-9] * 2\.106[0-9]" $log | wc -l`
TEST[19]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[19]=6
ERROR[19]="FREQUENCIES NOT CORRECT"

# Geometry optimization:
CRIT1=`$GREP "Geometry converged in * 3( |  )iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-224\.518757 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * (\-0|\-)\.000233 a\.u\." $log | wc -l`
TEST[20]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[20]=3
ERROR[20]="GEOMETRY OPTIMIZATION FAILED"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
